package tongwentongshu.com.app.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;
import java.util.List;

import tongwentongshu.com.app.R;
import tongwentongshu.com.app.contract.SelectLocationContract;
import tongwentongshu.com.app.layout.PoiView;
import tongwentongshu.com.app.map.LocationOnSuccess;
import tongwentongshu.com.app.map.MapLocation;

/**
 * Created by XiongBin on 2017/6/29.
 */

public class SelectLocationPresenter implements SelectLocationContract.Presenter, PoiSearch.OnPoiSearchListener {

    private SelectLocationContract.View selectlocation;
    private MapLocation mapLocation;//定位对象
    private Context mContext;
    private PoiSearch.Query query;// Poi查询条件类
    private PoiSearch poiSearch;
    private ArrayList<PoiItem> poiItems;// poi数据

    public SelectLocationPresenter(SelectLocationContract.View selectlocation) {
        this.selectlocation = selectlocation;
    }

    @Override
    public void initLocation(Activity act,Intent intent) {
        mContext = act;
        poiItems = intent.getExtras().getParcelableArrayList("poi_list");
        List<PoiDetails> domes = new ArrayList<PoiDetails>();
        for (int i = 1; i < poiItems.size(); i++) {
            PoiItem poi = poiItems.get(i);
            PoiDetails d = new PoiDetails(poi.getTitle(), poi.getSnippet() + poi.getDistance());
            domes.add(d);
        }
        ListAdapter adapter = new CustomArrayAdapter(
                mContext, domes);
        selectlocation.setLocationAdapter(adapter, poiItems.get(0));
//
//        mapLocation = new MapLocation(act);
//        mapLocation.toLocation(locationOnSuccess);
    }

    @Override
    public PoiItem getPoi(int index) {
        return poiItems.get(index);
    }


    //定位完毕回调
    public LocationOnSuccess locationOnSuccess = new LocationOnSuccess() {
        @Override
        public void onLocation(AMapLocation location) {
            if (null != location) {
                doSearchQuery(location);
            }

        }
    };

    /**
     * 开始进行poi搜索
     */
    protected void doSearchQuery(AMapLocation location) {
        query = new PoiSearch.Query("", "", "");// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query.setPageSize(20);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);// 设置查第一页

        if (location != null) {
            poiSearch = new PoiSearch(mContext, query);
            poiSearch.setOnPoiSearchListener(this);
            poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(location.getLatitude(), location.getLongitude()), 5000, true));//
            // 设置搜索区域为以lp点为圆心，其周围5000米范围
            poiSearch.searchPOIAsyn();// 异步搜索
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int rcode) {

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int rcode) {
        if (rcode == AMapException.CODE_AMAP_SUCCESS) {
            if (poiResult != null && poiResult.getQuery() != null) {// 搜索poi的结果
                poiItems = poiResult.getPois();// 取得第一页的poiitem数据，页数从数字0开始

            }
        }
    }

    public static class PoiDetails {
        private final String title;
        private final String description;

        public PoiDetails(String title, String description) {
            super();
            this.title = title;
            this.description = description;
        }
    }

    public static class CustomArrayAdapter extends ArrayAdapter<PoiDetails> {
        public CustomArrayAdapter(Context context, List<PoiDetails> demos) {
            super(context, R.layout.view_poi, R.id.title, demos);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PoiView featureView;
            if (convertView instanceof PoiView) {
                featureView = (PoiView) convertView;
            } else {
                featureView = new PoiView(getContext());
            }
            PoiDetails demo = getItem(position);
            featureView.setTitle(demo.title);
            featureView.setDescription(demo.description);
            return featureView;
        }
    }
}
